﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using SYH.System.Model;

namespace admin.syh876.com.Controllers
{
    public class AdminMgrController : Controller
    {
        //----管理员信息管理------//
        // GET: /Admin/AdminMgr/

        #region 管理员信息管理
        /// <summary>
        /// 管理员列表
        /// </summary>
        /// <returns></returns>
        public ActionResult AdminList()
        {
            DataTable adminDt = SYH.System.BLL.SystemAdmin.GetList("", true).Tables[0];
            return View(adminDt);
        }

        /// <summary>
        /// 添加、修改管理员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AdminDetail(int id = 0)
        {
            SystemAdmin model_SystemAdmin = SYH.System.BLL.SystemAdmin.GetModel(id);
            if (model_SystemAdmin == null)
                model_SystemAdmin = new SystemAdmin();

            DataTable dt_Role = SYH.System.BLL.SystemRole.GetList("").Tables[0];
            ViewBag.Role = dt_Role;
            return View(model_SystemAdmin);
        }

        //保存
        [HttpPost]
        public ActionResult AdminSave(SystemAdmin model)
        {
            //过滤非法字符
            model.UserName = Common.StringManager.ClearStrictSql(model.UserName).Trim();
            //密码MD5加密
            model.PassWord = Common.MD5.Md5En(model.PassWord);
            if (model.Id > 0)//修改
            {
                if (SYH.System.BLL.SystemAdmin.Update(model))
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            else//增加
            {
                if (SYH.System.BLL.SystemAdmin.Add(model) > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
        }
        //删除
        [HttpPost]
        public ActionResult AdminDelete(int id = 0)
        {
            if (SYH.System.BLL.SystemAdmin.Delete(id))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion


        //----角色管理------//

        #region 角色管理
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleList()
        {
            DataTable RoleDt = SYH.System.BLL.SystemRole.GetList(" Status>0 ").Tables[0];
            return View(RoleDt);
        }

        public ActionResult RoleDetail(int id = 0)
        {
            #region 角色信息
            SystemRole model_SystemRole = SYH.System.BLL.SystemRole.GetModel(id);
            if (model_SystemRole == null)
                model_SystemRole = new SystemRole();

            if (id > 0)
                model_SystemRole.ActionList = model_SystemRole.ActionCollection.Split('|').ToList();
            #endregion

            DataTable ControllerDt = SYH.System.BLL.SystemController.GetList("").Tables[0];
            IList<SystemController> ControllerList = Common.ModelHelper.FillModelsFromDataTable<SystemController>(ControllerDt);
            ViewBag.ControllerList = ControllerList;

            DataTable ActionDt = SYH.System.BLL.SystemAction.GetList("").Tables[0];
            IList<SystemAction> ActionList = Common.ModelHelper.FillModelsFromDataTable<SystemAction>(ActionDt);
            ViewBag.ActionList = ActionList;


            return View(model_SystemRole);
        }

        [HttpPost]
        public ActionResult RoleSave(SystemRole model)
        {
            //过滤非法字符
            model.RoleName = Common.StringManager.ClearStrictSql(model.RoleName).Trim();
            if (model.Id > 0)//修改
            {
                if (SYH.System.BLL.SystemRole.Update(model))
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            else//增加
            {
                if (SYH.System.BLL.SystemRole.Add(model) > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
        }

        [HttpPost]
        public ActionResult RoleDelete(int id = 0)
        {
            if (SYH.System.BLL.SystemRole.Delete(id))
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }
        #endregion

    }
}
